<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<%@ page import="java.util.*"%>
<%@ page import="ils.web.model.*"%>
<%@ page import="ils.services.services.*"%>
<%@ page import="org.apache.log4j.Logger"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<link rel="stylesheet" type="text/css" href="main.css" />
<title>Book Details</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	if($("p#status").html() != "true")
	{
		$("div#div1").hide();
	}else
	{
		$("div#div2").hide();
	}
	$("button#button1").click(function(){
		var rating = $("input#rating").text();
		var comment = $("textarea#review").text();
		var url = "createReview"
		$.post("createReview", $("form#form1").serialize(), function(data){}, "json");
		$("p#rating").html(rating);
		$("p#review").html(comment);
		$("div#div1").hide();
		$("div#div2").show();
	});
});
</script>
<script type="text/javascript">
function requestBook(id)
{
	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open("GET", "issue?action=request&bookId="+id, false);
	xmlHttp.send();
	var text = xmlHttp.responseText;
	document.getElementById("result").innerHTML=text;
}
</script>
</head>
<body>
	<%@include file="header.jsp"%>
	<article> <s:set name="jbook" value="book" scope="request" />
	<s:set name="jbookId" value="bookId" scope="request" /> <s:set
		name="reviews" value="reviews" scope="request" /> <s:set
		name="ownReview" value="ownReview" scope="request" /> <%
     Logger logger = Logger.getLogger(this.getClass());
     logger.debug("inside bookDetails");
 %> <c:choose>
		<c:when test="${empty jbook }">
		No Book found for the book id: ${jbookId } !
	</c:when>
		<c:otherwise>
		Title: ${jbook.title } <br />
		Author: ${jbook.author } <br />
        Rating: ${jbook.ratingsSum/jbook.ratingsCount} out of ${jbook.ratingsCount} ratings <br />
        Topics: ${jbook.topics} <br />
        Available ${jbook.availableCount} out of ${jbook.totalCount} books <br />
			<button type="button" onclick="requestBook(${jbook.id})">Request
				this Book</button>
			<div id="result"></div>
		</c:otherwise>
	</c:choose>

    <p>Last 10 activities on this book</p> <br />
    <c:choose>
        <c:when test="${not empty issues}">
            <table border="1">
                <tr><th>Issue Date</th><th>Return Date</th><th>User</th></tr>
                <c:forEach var="issue" items="${issues}">
                    <tr>
                        <td>${issue.issueDate}</td>
                        <td>${issue.returnDate}</td>
                        <td>${issue.userName}</td>
                    </tr>
                </c:forEach>
            </table>
        </c:when>
    </c:choose>

    Reviews <br />
	<p class="hidden" id="status">${empty ownReview}</p>
	<div id="div1">
		<form id="form1">
			<input type="hidden" name="bookId" value="${jbookId}" /> Your
			Rating: <input id="rating" type="text" name="rating" /> <br /> Your
			Comment:
			<textarea id="review" name="review" rows="5"></textarea>
		</form>
		<button id="button1">Submit</button>
		<br />
		<p id="response"></p>
	</div>
	<div id="div2">
		Your Rating:
		<p id="rating">${ownReview.rating }</p>
		<br /> Your Comment:
		<p id="review">${ownReview.review }</p>
	</div>
	<c:choose>
		<c:when test="${empty reviews }">
		No Reviews found for this book!
	</c:when>
		<c:otherwise>
			<table border="1">
				<c:forEach var="review" items="${reviews}">
					<tr>
						<td>User: ${review.userName} <br />
                            Rating: ${review.rating} <br />
                            Review: ${review.review} <br />
						</td>
					</tr>
				</c:forEach>
			</table>
		</c:otherwise>
	</c:choose> </article>
	<%@include file="footer.jsp"%>
</body>
</html>